#ifndef SERVER_H_
#define SERVER_H_

#include <QObject>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QNetworkAccessManager>
#include "../RpcServer/JZRpcServer.h"

class Server : public QObject
{
    Q_OBJECT

public:
    Server();
    ~Server();
    
    bool start(int port);

protected slots:
    void onNetConnect(int id);
    void onNetRecv(int id,QJsonObject obj);
    void onNetDisconnect(int id);

    void onTextReply();
    void onTextReplyFinish();
protected:
    struct ClientData
    {
        ClientData();
        void clear();
        void closeOllamaReply();

        int session;
        int chatId;
        QString context;        
        QNetworkReply *ollamaReply;
        int ollamaRecv;
    };

    bool isValidCliend(int net_id);
    void dealOllamaPack(QNetworkReply *reply);
    void sendOllamaReq(int net_id,QString text);
    void sendPack(int net_id,const QJsonObject &obj);

    QNetworkAccessManager *m_manager;    
    JZRpcServer m_server;    
    QMap<int,QSharedPointer<ClientData>> m_clients;
    JZRpcDataManager m_dataManager;
};


#endif